package com.example.demo1.service.ipml;

import com.example.demo1.domain.Student;
import com.example.demo1.exception.REnum;
import com.example.demo1.exception.RException;
import com.example.demo1.repository.StudentRepository;
import com.example.demo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImplService implements StudentService{

    @Autowired
    StudentRepository studentRepository;
    @Override
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    @Override
    public Page<Student> findAll(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }
    public Page<Student> findAll(Example<Student> student ,Pageable pageable) {
        return studentRepository.findAll(student,pageable);
    }

    @Override
    public Student insert(Student student) {
        return studentRepository.save(student);
    }

    @Override
    public Student update(Student student) {
        return studentRepository.save(student);
    }

    @Override
    public void deleteById(Integer id) {    }
    @Override
    public Student findById(Integer id) {
        return null;
    }


    public Student findByNameAndPassword(String name,String password) throws Exception{
        List<Student> byNameAndPassword = studentRepository.findByNameAndPassword(name,password);
        if (byNameAndPassword.size()>0){
            return byNameAndPassword.get(0);
        }else{
            throw new RException(REnum.LOGIN_ERR);
        }
    }
}
